﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Net.NetworkInformation;

namespace SilverlightApplication1
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			// Required to initialize variables
			InitializeComponent();

            btnAccion.Click += new RoutedEventHandler(btnAccion_Click);
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);

            ActualizarEstados();
            inicializarBoton();
		}

        void btnAccion_Click(object sender, RoutedEventArgs e)
        {
            if (App.Current.IsRunningOutOfBrowser)
            {
                App.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
                App.Current.CheckAndDownloadUpdateAsync();
            }
            else
            {
                App.Current.Install();
            }
        }

        void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
        {
            if (e.UpdateAvailable)
            {
                MessageBox.Show("La applicación ha sido actualizada, por favor reinicia");
            }
            else
            {
                MessageBox.Show("No hay actualizaciones disponibles");
            }
        }

        void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            cambiaEstado(txtConexion, NetworkInterface.GetIsNetworkAvailable());
        }

        private void inicializarBoton()
        {
            if (App.Current.IsRunningOutOfBrowser)
            {
                btnAccion.Content = "Actualizar";
            }
            else
            {
                if (App.Current.InstallState == InstallState.NotInstalled)
                {
                    btnAccion.Content = "Instalar";
                }
                else
                {
                    btnAccion.Content = "Aplicación instalada";
                    btnAccion.IsEnabled = false;
                }
                
            }
        }

        private void ActualizarEstados()
        {
            cambiaEstado(txtOOB, App.Current.IsRunningOutOfBrowser);
            cambiaEstado(txtConexion, NetworkInterface.GetIsNetworkAvailable());
        }

        public void cambiaEstado(TextBlock texto, bool estado)
        {
            if (estado)
            {
                texto.Text = "Si";
                texto.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                texto.Text = "No";
                texto.Foreground = new SolidColorBrush(Colors.Red);
            }
        }
	}
}